package controller;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.UserBean;

import model.UserDAO;

/**
 * Servlet implementation class SearchUserServlet
 */
@WebServlet("/SearchUserServlet")
public class SearchUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{	    
			//Set search parameters gathered from the form
			UserBean user = new UserBean();
			if (request.getParameter("searchedemail") != null)
				user.setEmail(request.getParameter("searchedemail"));
			else
				user.setEmail("");
			if (request.getParameter("searchedname") != null)
				user.setName(request.getParameter("searchedname"));
			else
				user.setName("");
			if (request.getParameter("searchedcity") != null )
				user.setCity(request.getParameter("searchedcity"));
			else
				user.setCity("");
			if (request.getParameter("searchedcountry") != null)
				user.setCountry(request.getParameter("searchedcountry"));
			else
				user.setCountry("");

			System.out.println(user.getName());
			String searchtype = request.getParameter("search");

			//Perform the search query
			ArrayList<UserBean> searchResults = UserDAO.search(user);

			HttpSession session = request.getSession(true);
			session.setAttribute("searchResults", searchResults); 
			session.setAttribute("searchTermsUserBean", user);
			session.setAttribute("searchType", searchtype);
			response.sendRedirect("searchresults.jsp");      		
		} 


		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}	
	}
}
